package com.woniuxy.service.impl;


import com.woniuxy.dao.QuestionInventoryMapper;
import com.woniuxy.dao.QuestionOptionMapper;
import com.woniuxy.dao.QuestionTopicAnalysisMapper;
import com.woniuxy.dao.QuestionTopicMapper;

import com.woniuxy.entity.AddQuestionEntity;
import com.woniuxy.entity.QuestionOption;
import com.woniuxy.entity.QuestionTopic;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.QuestionService;
import org.apache.ibatis.annotations.Case;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionTopicMapper questionDao;
    @Autowired
    private QuestionOptionMapper questionOptionDao;
    @Autowired
    private QuestionInventoryMapper questionInventoryDao;


    @Override
    @Transactional
    public ResponseEntity addQuestionDan(AddQuestionEntity addQuestionEntity) {
        QuestionTopic questionTopic = new QuestionTopic();
        questionTopic.setAnalysis(addQuestionEntity.getAnalysis());
        questionTopic.setTopic(addQuestionEntity.getTopic());
        questionTopic.setTid(addQuestionEntity.getTid());
        questionTopic.setIid(questionInventoryDao.findIdByAdd(addQuestionEntity));
        System.out.println(questionTopic.toString());
        int i = questionDao.insertq(questionTopic);
        int num = questionTopic.getId();
        System.out.println(num);
        QuestionOption questionOption = new QuestionOption();
        List<String> ops = new ArrayList<>();
        ops.add(addQuestionEntity.getOption1());
        ops.add(addQuestionEntity.getOption2());
        ops.add(addQuestionEntity.getOption3());
        ops.add(addQuestionEntity.getOption4());
        if (addQuestionEntity.getDaan() instanceof ArrayList) {
            ArrayList<String> daans = (ArrayList<String>) addQuestionEntity.getDaan();
            int k = 1;
            for (String op : ops) {
                questionOption.setOpt(op);
                questionOption.setTopicId(num);
                questionOption.setStatus("n");
                for (String daan : daans) {
                    Integer daann = Integer.parseInt(daan);
                    if (daann.equals(k)) {
                        questionOption.setStatus("y");
                    }
                }
                    questionOptionDao.insert(questionOption);
                    k++;
            }
        } else {
            Integer daan = Integer.parseInt((String) addQuestionEntity.getDaan());
            int k = 1;
            for (String op : ops) {
                questionOption.setOpt(op);
                questionOption.setTopicId(num);
                if (daan.equals(k)) {
                    questionOption.setStatus("y");
                } else {
                    questionOption.setStatus("n");
                }
                questionOptionDao.insert(questionOption);
                k++;
            }
        }
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public ResponseEntity addOption(QuestionOption questionOption) {
        int i = questionOptionDao.insert(questionOption);


        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override

    public String[] findSub() {
        return questionInventoryDao.findSub();
    }

    @Override
    public String[] findGrade() {
        return questionInventoryDao.findGrade();
    }

    @Override
    public List<QuestionTopic> findAllQ(Map map){
        return questionDao.findAllQ(map);
    }

    @Override
    public List<QuestionTopic> findQbyid(Integer id) {
        return questionDao.findQbyid(id);
    }

    @Override
    public ResponseEntity deleteQbyId(Integer id){
        int i = questionDao.deleteQById(id);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public List<QuestionTopic> findQubyid(Integer id) {
        return questionDao.findQubyid(id);
    }

}
